#!/bin/bash
#导入配置文件
source /usr/local/src/mysql/package/install.cnf

#检查md5值
function check_md5(){

if [ "$1" = "$2" ]
then
echo "-----------------------------------'$3'packag ok ----------------------------------------"
else
echo "-----------------------------------'$3' packag error pleas reinstall ----------------------------------------" >> $package_dir/ad_install.log
exit
fi
}

#mysql rpm安装包检查
function check_mysql_rpm(){
#检查是否存在mariadb的rpm,是则删除
ma=`rpm -qa |grep mariadb`
$mariadb_check=${ma:0:7}
echo $mariadb_check

for i in $ma
do
        echo "exist mariadb.rpm,will remove it"
        rpm -e $i --nodeps
done


#检查是否与mysql8的rpm存在
a=`rpm -qa | grep "$1"`
b=${a:16}
c=${b//./}
check_result=${c//-/}
echo $check_result
#如果模块存在则继续，如果模块不存在则安装，安装后若仍旧判断为不存在，则报错退出
if (($check_result==$2))
then
        echo "$1  exist,continue"
else
        rpm -ivh $3 --nodeps
        a=`rpm -qa | grep "$1"`
        b=${a:16}
        c=${b//./}
        check_result=${c//-/}
        if (($check_result==$2))
        then
                echo "$1  exist,continue"
        else
                echo "something  wrong with $1,exit" >> $package_dir/ad_install.log
        fi
fi
}

#安装mysql8.0
function mysql_install(){
echo "start install mysql8.0"
#判断安装包是否存在，不存在则报错
if [ ! -f $package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar ]
then
        echo "mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar not exist,please check" >> $package_dir/ad_install.log
        echo "quit install"
        exit
fi

cd $package_dir
#检查mysql安装包的md5值
mysql_md5_now=`md5sum "$package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar" | awk '{print $1}'`
check_md5 $mysql_md5_now $mysql_md5_origin mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

#解压安装包
echo "unpack mysql package"
tar -xvf $package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
mkdir $package_dir/mysql_rpm
mv mysql-community* $package_dir/mysql_rpm
#
cd $package_dir/mysql_rpm
#
##调用方法检查mysql的各个rpm模块是否存在，不存在则安装
echo "install mysql rpms"

check_mysql_rpm mysql-community-common-8.0.19-1.el7.x86_64 common80191el7x86_64 mysql-community-common-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-libs-8.0.19-1.el7.x86_64 libs80191el7x86_64 mysql-community-libs-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-libs-compat-8.0.19-1.el7.x86_64 libscompat80191el7x86_64 mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-client-8.0.19-1.el7.x86_64 client80191el7x86_64 mysql-community-client-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-server-8.0.19-1.el7.x86_64 server80191el7x86_64 mysql-community-server-8.0.19-1.el7.x86_64.rpm


#修改配置文件，主要修改内容有端口、安装路径、错误日志路径等
mv /etc/my.cnf /etc/my.cnf.bak
cp $package_dir/my.cnf /etc/my.cnf
sed -ri 's#(port            =)[^ ]*#\port            ='$mysql_port'#' /etc/my.cnf
sed -ri 's#(log-error       =)[^ ]*#log-error       ='$mysql_errlog'#' /etc/my.cnf
sed -ri 's#(basedir         =)[^ ]*#\basedir         ='$mysql_basedir'#' /etc/my.cnf
sed -ri 's#(datadir         =)[^ ]*#\datadir         ='$mysql_datadir'#' /etc/my.cnf
sed -ri 's#(socket          =)[^ ]*#\socket          ='$mysql_basedir/mysql.sock'#' /etc/my.cnf
#

#创建mysql安装路径
if [ ! -d $mysql_basedir ]
then
        mkdir -p $mysql_basedir
else
        rm -rf $mysql_basedir/*
fi
chown -R mysql:mysql $mysql_basedir

#mysql指定参数初始化
echo "start initialize mysql"
/sbin/mysqld --user=mysql --lower-case-table-names=1 --initialize --basedir=$mysql_basedir --datadir=$mysql_datadir

chown -R mysql:mysql $mysql_basedir

#复制errmsg.sys文件到指定路径，防止报错
mkdir  -p $mysql_basedir/share/mysql-8.0
cp /usr/share/mysql-8.0/english/errmsg.sys $mysql_basedir/share/mysql-8.0/
chown -R mysql:mysql $mysql_basedir/share
chmod 744 $mysql_basedir/share/mysql-8.0/errmsg.sys

#启动mysql并设置开机自启动
echo "start mysql"
systemctl start mysqld
systemctl enable mysqld

#获取mysql初始密码
mysql_passwd_origin=`cat "$mysql_errlog" | grep password | head -1 | rev  | cut -d ' ' -f 1 | rev`
echo $mysql_passwd_origin

#使用初始密码连接mysql并更改密码，root用户授权
mysql --connect-expired-password  -uroot -p"$mysql_passwd_origin"  >tables.txt  <<EOF
alter user 'root'@'localhost' identified  by '$mysql_passwd';
create user 'root'@'%' identified  by '$mysql_passwd';
grant all privileges on *.* to 'root'@'%' with grant option;
create user 'root'@'127.0.0.1' identified  by '$mysql_passwd';
grant all privileges on *.* to 'root'@'127.0.0.1' with grant option;
flush privileges;
quit
EOF
}


